﻿<?php
/*********************************************************************\
* This File is a part of BH-PANEL (Breizh-Heberg Panel v2.1)
* Copyright (C) 2008-2009 the BH Developpers : Vincent Giersch <mail@vincordi.fr>, Cyprien Laleau <fanning.fr@gmail.com> and Edwin Cabiten <marmottes44@hotmail.fr>
* See file AUTHORS to get more informations

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
/*********************************************************************/

if (isset($_POST['ajax'])) {
	define('IN', '1');
	define('REQUEST_AJAX', '1');
	include ('../php/lib/common.php');
	connexionbdd();
	$tpl = new gagatemplate('../');	                                          
	$id_maint = intval($_POST['id_maint']);
	if (!isset($_POST['send'])) {
		if ($id_maint == 0) {		
			$titre_d = "";
			$etat_d = 0;
			$severite_d = 0;
			$cat_d = 0;
			$type_d = 0;
			$fermer_d = 0;
			$priorite_d = 0;
			$details_d = "";
			$pourcent_d = 0;
		}
		else {
			$req = mysql_fetch_array(query("SELECT * FROM maintenance WHERE id='".$id_maint."'"));
			$titre_d = $req['titre'];
			$etat_d = $req['etat'];
			$severite_d = $req['level_severite'];
			$cat_d = $req['categorie'];
			$type_d = $req['type'];
			$fermer_d = $req['fermer'];
			$priorite_d = $req['priorite'];
			$details_d = $req['details'];
			$pourcent_d = intval($req['pourcent_fini']);
        }	
		$tpl->assign(array(	'id_maint' => $id_maint,
							'titre_d' => stripslashes($titre_d),
							'etat_d' => $etat_d,
							'severite_d' => $severite_d,
							'cat_d' => $cat_d,
							'type_d' => $type_d,
							'fermer_d' => $fermer_d,
							'priorite_d' => $priorite_d,
							'details_d' => stripslashes($details_d),
							'pourcent_d' => $pourcent_d,
							'error' => 0));
		$tpl->parse('admin-maintenance-req.html');
	}
	else {
		$titre = mysql_real_escape_string($_POST['titre']);
		$details = mysql_real_escape_string($_POST['details']);
		$cat = intval($_POST['cat']);
		$severite = intval($_POST['severite']);
		$type = intval($_POST['type']);
		$priorite = intval($_POST['priorite']);
		$etat = intval($_POST['etat']);
		$pourcent = intval($_POST['pourcent']);
		$fermer = intval($_POST['fermer']);
		if ($titre != "" && $titre != " " && $details != "" && $details != " ") {	
			if ($cat != 0 && $severite != 0 && $type != 0 && $priorite != 0 && $etat != 0) {			
				if ($fermer == 1) {
					$d_fermer = time();
				}
				else {
					$d_fermer = NULL;
				}	
				
				if ($_POST['id_maint'] == 0) {	
					query("INSERT INTO maintenance (id, type, categorie, titre, fermer, date_fermer, details, level_severite, etat, pourcent_fini, priorite) VALUES('', '".$type."', '".$cat."', '".$titre."', '".$fermer."', '".$d_fermer."', '".$details."', '".$severite."', '".$etat."', '".$pourcent."', '".$priorite."')");
					$tpl->assign(array('error' => 1));
				}
				else {
					query("UPDATE maintenance SET type = '".$type."', categorie = '".$cat."', titre = '".$titre."', fermer = '".$fermer."', date_fermer = '".$d_fermer."', details = '".$details."', level_severite = '".$severite."', etat = '".$etat."', pourcent_fini = '".$pourcent."', priorite = '".$priorite."' WHERE id = '".intval($_POST['id_maint'])."'");	
					$tpl->assign(array('error' => 2));
				}
					
				$req_maint = query("SELECT * FROM maintenance ORDER BY id DESC");
				
				if (($nb_maint = mysql_num_rows($req_maint)) > 0) {
		
					$etat_array = array('1' => 'Annuler',
										'2' => 'En cours',
										'3' => 'En pause',
										'4' => 'Terminé',
										);
		
					while($result_maint = mysql_fetch_array($req_maint)) {
			
					$etat = $result_maint['etat'];
			
					$tpl->assign_array('list_maint', array( 'id' => $result_maint['id'],
															'etat' => $etat_array[$etat],
															'date' => $result_maint['date_ouvert'],	
															'titre' => $result_maint['titre'],
															));
													
					}
		
					$tpl->assign(array('nb_maint' => $nb_maint));
					$tpl->parse('admin-maintenance-inc.html');
				}
				else {
					$tpl->assign(array('nb_maint' => 0));
					$tpl->parse('admin-maintenance-inc.html');
				}
			}
			else {
				$tpl->assign(array(	'id_maint' => $_POST['id_maint'],
						'titre_d' => stripslashes($titre),
						'etat_d' => $etat,
						'severite_d' => $severite,
						'cat_d' => $cat,
						'type_d' => $type,
						'fermer_d' => $fermer,
						'priorite_d' => $priorite,
						'details_d' => stripslashes($details),
						'pourcent_d' => $pourcent,
						'error' => 1));
				$tpl->parse('admin-maintenance-req.html');
			}
		}
		else {
			$tpl->assign(array(	'id_maint' => $_POST['id_maint'],
						'titre_d' => stripslashes($titre),
						'etat_d' => $etat,
						'severite_d' => $severite,
						'cat_d' => $cat,
						'type_d' => $type,
						'fermer_d' => $fermer,
						'priorite_d' => $priorite,
						'details_d' => stripslashes($details),
						'pourcent_d' => $pourcent,
						'error' => 2));
			$tpl->parse('admin-maintenance-req.html');
		}							
	}
	deconnexionbdd();
}
elseif (isset($_POST['ajax1'])) {
	define('IN', '1');
	define('REQUEST_AJAX', '1');
	include ('../php/lib/common.php');
	connexionbdd();
	$tpl = new gagatemplate('../');
	
	if (isset($_POST['id']) && !empty($_POST['id'])) {
		$id_maint = intval($_POST['id']);
		$verif_exist = query("SELECT * FROM maintenance WHERE id = '".$id_maint."'");
	
		if (mysql_num_rows($verif_exist) == 1) {
		
			if (query("DELETE FROM maintenance WHERE id = '".$id_maint."'")) {
				$tpl->assign(array('error' => 3));
			}
			else
				$tpl->assign(array('error' => 4));
		}
		else
			$tpl->assign(array('error' => 5));
	}
	else
		$tpl->assign(array('error' => 6));
		
	$req_maint = query("SELECT * FROM maintenance ORDER BY id DESC");
				
	if (($nb_maint = mysql_num_rows($req_maint)) > 0) {
		
		$etat_array = array('1' => 'Annuler',
							'2' => 'En cours',
							'3' => 'En pause',
							'4' => 'Terminé',
							);
		
		while($result_maint = mysql_fetch_array($req_maint)) {
			
			$etat = $result_maint['etat'];
			
			$tpl->assign_array('list_maint', array( 'id' => $result_maint['id'],
													'etat' => $etat_array[$etat],
													'date' => $result_maint['date_ouvert'],	
													'titre' => $result_maint['titre'],
													));
													
		}
		
		$tpl->assign(array('nb_maint' => $nb_maint));
	}
	else {
		$tpl->assign(array('nb_maint' => 0));
	}
	$tpl->parse('admin-maintenance-inc.html');
}	
?>